package com.neusoft.orcode.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.neusoft.orcode.info.AuthIdInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

@Controller
public class BakController {

    // http://127.0.0.1:8080/qrcode1
    // 接收患者信息后转换为二维码
    @RequestMapping("/qrcode1")
//    @ResponseBody
    public void qrcode1(HttpServletResponse response) {
        BufferedImage image = null;
        String test = JSONObject.toJSONString(new AuthIdInfo("张雪", "13151516114"));
        System.out.println(test);
        try {
            // 生成黑白的二维码
            image = QrCodeGenWrapper.of(test).asBufferedImage();
            // 写入响应流
            ImageIO.write(image,"png", response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
//        return "qrcode";
    }

    // http://127.0.0.1:8080/test
    @RequestMapping(value = "/test", produces = "application/json", method = { RequestMethod.POST, RequestMethod.GET })
    public String test(RedirectAttributes ra) {
//        ModelAndView view = new ModelAndView();
//        view.setViewName("redirect:http://172.24.208.168/Default.aspx");

        System.out.println("到达test");
//        ra.addAttribute("auth_id", 1);
        ra.addFlashAttribute("auth_id", 1);
//        return "redirect:http://www.baidu.com";
        return "redirect:/getPatientInfo";
    }

    @RequestMapping("/b")
    public String test2() {
        return "BouncyCastleRSA";
    }

    // http://127.0.0.1:8080/a
    @PostMapping("/a")
    public String test1(@RequestParam("auth_id") String authId) {
        return "hpbrowser";
    }


}
